<?php
/*
	
*/
defined('BY_JYA') or exit('Access Denied');
global $_RQ,$_AR;

$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$return=array();
$return['diy']=$this->plugin_setting('diy');
$basic=$this->plugin_setting('basic');
$return['banner']=$basic['image'];
$return['basic']=$basic;
if($_RQ['op']=='main'){
	if(!$_RQ['id']){
		exi('没有找到该作品，请重新选择！','error');
	}
	$opus=pdo_get('jy_xiuvote_opus',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	
	$user = pdo_get('jy_xiuvote_user',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$opus['mid']));
	if(($user['isvip'] !=1 || $user['viptime'] < time()) && $basic['isfee_hongbao']){
		$user['ishongbao'] =0;
	}else{
		$user['ishongbao'] =1;
	}
	if($opus['mid'] == $_SESSION['uid']){
		$return['isme']=1;
	}else{
		$return['isme']=0;
	}
	if($opus['status'] !=1 && $return['isme']==0){
		//exi('该作品不公开，或者在审核！','error');
	}
	//是否关注
	$return['isfollow']=pdo_getcolumn('core_fans',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']),'follow');
	//添加一次浏览次数
	pdo_update('jy_xiuvote_opus',array('look +='=>1),array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	//进入作品详情，记录访问者浏览信息
	$islook=pdo_get('jy_xiuvote_looklist',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'oid'=>$_RQ['id']));
	if(empty($islook)){
		$insert=array(
			'uniacid'=>$_SESSION['uniacid'],
			'oid'=>$_RQ['id'],
			'mid'=>$_SESSION['uid'],
			'createtime'=>TIMESTAMP
		);
		pdo_insert('jy_xiuvote_looklist',$insert);
	}else{
		pdo_update('jy_xiuvote_looklist',array('createtime'=>TIMESTAMP),array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'oid'=>$_RQ['id']));
	}
	$opus['createtime']=date('Y-m-d H:i:s',$opus['createtime']);
	$opus_images=json_decode($opus['opus_images'],true);
	$this->session('id',$_RQ['id']);
	$return['user']=$user;
	$return['opus']=$opus;
	$return['islike']=$islook['islike'];
	$return['isfans']=pdo_getcolumn('jy_xiuvote_userlog',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'userid'=>$opus['mid']),'isfollow');
	$return['opus_images']=$opus_images;
	$return['sharedata']=array(
		'title'=>$opus['title'],
		'desc'=>$opus['description'],
		'link'=>custom_url('jy_xiuvote/o_detail//id='.$_RQ['id'],true),
		'imgUrl'=>ATTACHMENT_URL.$opus_images[0],
	);
	set_html('title',$opus['title']);
	exi($return);
}
if($_RQ['op']=='follow'){
	$id = $this->session('id');
	if(!$id){
		exi('没有找到该作品，请重新选择！','error');
	}

	$opus=pdo_get('jy_xiuvote_opus',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	if($opus['mid'] == $_SESSION['uid']){
		exi('自己不能关注自己！','error');
	}
	$follow=pdo_get('jy_xiuvote_userlog',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'userid'=>$opus['mid']));
	if($follow){
		if($follow['isfollow'] !=1){
			pdo_update('jy_xiuvote_userlog',array('isfollow'=>1,'createtime'=>TIMESTAMP),array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'userid'=>$opus['mid']));
			exi('关注成功');
		}else{
			pdo_update('jy_xiuvote_userlog',array('isfollow'=>0),array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'userid'=>$opus['mid']));
			exi('已取消关注');
		}
	}else{
		$insert=array(
			'uniacid'=>$_SESSION['uniacid'],
			'userid'=>$opus['mid'],
			'mid'=>$_SESSION['uid'],
			'isfollow'=>1,
			'createtime'=>TIMESTAMP
		);
		pdo_insert('jy_xiuvote_userlog',$insert);
	}
	exi('关注成功');
}
if($_RQ['op']=='shoucang'){
	$id = $this->session('id');
	if(!$id){
		exi('没有找到该作品，请重新选择！','error');
	}
	$opus=pdo_get('jy_xiuvote_opus',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	$shoucang=pdo_get('jy_xiuvote_shoucang',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'oid'=>$id));
	if($shoucang){
		if($shoucang['isshoucang'] !=1){
			pdo_update('jy_xiuvote_shoucang',array('isshoucang'=>1,'createtime'=>TIMESTAMP),array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'oid'=>$id));
			exi('收藏成功');
		}else{
			pdo_update('jy_xiuvote_shoucang',array('isshoucang'=>0),array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'oid'=>$id));
		   exi('取消收藏');
		}
	}else{
		$insert=array(
			'uniacid'=>$_SESSION['uniacid'],
			'oid'=>$id,
			'mid'=>$_SESSION['uid'],
			'isshoucang'=>1,
			'createtime'=>TIMESTAMP
		);
		pdo_insert('jy_xiuvote_shoucang',$insert);
	}
	exi('收藏成功');
}
if($_RQ['op']=='like'){
	$id = $this->session('id');
	$postdata=get_postdata();
	if(!$id){
		exi('没有找到该作品，请重新选择！','error');
	}
	$looklist=pdo_get('jy_xiuvote_looklist',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'oid'=>$id));
	if($looklist){
		if($looklist['islike'] !=1){
			pdo_update('jy_xiuvote_opus',array('likes +='=>1),array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
			pdo_update('jy_xiuvote_looklist',array('islike'=>1),array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']));
			exi('点赞成功');
			
		}else{
			exi('不能取消点赞','error');
			//pdo_update('jy_xiuvote_opus',array('like -='=>1),array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
			//pdo_update('jy_xiuvote_looklist',array('islike'=>0),array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'oid'=>$id));
		}
	}
	
}
if($_RQ['op']=='setmsg'){
	$member=pdo_get('core_members',array('id'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),array('credit1','credit2','mobile','groupid','avatar','nickname'));
	$postdata=get_postdata();
	$data=array(
		'content'=>trim($postdata['msg']),
		'username'=>$member['nickname'],
		'uid'=>$_SESSION['uid'],
		'uniacid'=>$_SESSION['uniacid'],
		'createtime'=>time(),
	);
	pdo_insert('core_account_message',$data);
	exi('评论成功！');
}	
if($_RQ['op']=='sendhb'){
	$id=$_RQ['id'];
	if(!$_RQ['id']){
		$id=$this->session('id');
	}
	if(!$id){
		exi('未正确获取作品id','error');
	}
	
	$post=array();
	$post['money']=0;
	 $user=pdo_get('jy_xiuvote_user',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']));
	 $opus=pdo_get('jy_xiuvote_opus',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	if($_RQ['postdata']){
	   $postdata=get_postdata();
	   $money=$postdata['money'];
	   $username=$postdata['username'];
		if($money<=1){
		   exi('红包金额需大于1元','error');
	   }
	   if($money>200){
		   exi('单个红包金额最多不超过200元','error');
	   }
	   if(!$username){
		  // exi('请输入发送人姓名！','error');
	   }
	   //创建红包订单
	   $insert=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$_SESSION['uid'],
			'oid'=>$id,
			'omid'=>$opus['mid'],
			'fee'=>$money,
			'ordersn'=>get_ordersn('jy_xiuvote_redorder','ordersn'),
			'username'=>$username,
			'ispay'=>0,
			'status'=>0,
			'createtime'=>TIMESTAMP
	   );
	   pdo_insert('jy_xiuvote_redorder',$insert);
	   //支付
	   $id=pdo_insertid();
	   $params['fee']=$money;
	    $params['title']='发送红包订单';
		$params['id']=$id;
		$params['plugin']='jy_xiuvote';
		$params['pay_type']='wechat';//可选，此项作用为指定支付方式
		$params['no_type']='credit';//可选，此项作用为指定不允许使用哪种支付方式
		$params['back_url']='jy_xiuvote/o_detail/payresult/';
		$return['payid']=cfc('pay')->create_pay_order($params);
	}
	$return['post']=$post;
	exi($return);
}

if($_RQ['op']=='payresult'){
	$payresult=$_RQ['pay_result'];
	if(!$payresult){
		exi('支付失败，未收到支付结果！','error');
	}
	if($payresult['status']!=1){
		exi('支付失败！','error');
	}
	$order=pdo_get('jy_xiuvote_redorder',array('id'=>$payresult['id'],'uniacid'=>$_SESSION['uniacid']));
	if(!$order){
		exi('支付失败！发送红包订单不存在！','error');
	}
	if($order['fee']!=$payresult['fee']){
		exi('支付失败！发送红包订单金额不匹配！','error');
	}
	pdo_update('jy_xiuvote_redorder',array('ispay'=>1,'fee'=>$payresult['fee']),array('id'=>$order['id']));
	//更新发红包额
	pdo_update('jy_xiuvote_user',array('money_pay +='=>$order['fee']),array('mid'=>$_SESSION['uid']));
	//发红包
	//收红包人的信息
	$suser=pdo_get('core_fans',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$order['omid']));
	//发送红包
	
	if($basic['isconfirm']){
		exi('发送成功');
	}
	$arr['openid'] = $suser['openid'];
	$arr['hbname'] = '场景投票';
	$arr['body'] = "场景投票红包";
	$arr['fee'] = $order['fee'];
	$arr['mchid'] = $basic['mchid'];
	$arr['mch_appid'] = $basic['appid'];
	$arr['username'] = $order['username'];
	$res = sendhongbaoto($arr);
 	if($res['result_code'] == 'SUCCESS' && $res['return_code'] == 'SUCCESS'){
		pdo_update('jy_xiuvote_redorder',array('status'=>1),array('id'=>$order['id']));
		pdo_update('jy_xiuvote_opus',array('money +='=>$order['fee'],'red_num +='=>1),array('id'=>$order['oid']));
		//更新收红包额
		pdo_update('jy_xiuvote_user',array('money_get +='=>$order['fee']),array('mid'=>$order['omid']));
		//发送消息
		$newmember = pdo_get('jy_xiuvote_user',array('id'=>$_SESSION['uid']),'nickname');
		$msg =array('username'=>$newmember['nickname']);
		//set_sms($newmember['mobile'],$msg,1,$_SESSION['uid']);
		set_websms($newmember['nickname'].':您收到一个新的红包！',0,$order['omid'],'');
		//发送模板消息
		$this->notice('setred',$_SESSION['uid'],$order['o_id'],$order['id']);
		exi('红包转发成功','success');
	}
	else{
		pdo_update('jy_xiuvote_redorder',array('status'=>2,'error_msg'=>$res['return_msg'].'/'.$res['err_code_des']),array('id'=>$order['id']));
		exi('红包发送失败','error');
	}
}